package game.entities;

import game.InputHandler;
import game.gfx.Colors;
import game.gfx.Font;
import game.gfx.Screen;
import game.level.Level;

public class PodGuy extends Player {

	private int color = Colors.get(-1, 111, 132, 543);
	private int wandColor = Colors.get(-1, 532, 500, -1);
	public PodGuy(Level level, int x, int y, InputHandler input,
			String username, int fallSpdForDmg) {
		super(level, x, y, input, username, fallSpdForDmg, 2);
	}

	public void tick() {
		super.tick();
	}
	
	public void render(Screen screen) {
		timeNow = System.currentTimeMillis();
		int xTile = 0;
		int yTile = 22;
		int walkingSpeed = 4;
		//int flipTop = (numSteps >> walkingSpeed) & 1;
		//int flipBottom = (numSteps >> walkingSpeed) & 1;
		int flipTop = 0;
		int flipBottom = 0;
		if(input.mouse.mousePosX()<330){
			flipTop = 1;
			flipBottom = 1;
		}



		int modifier = 8 * scale;
		int xOffset = x - modifier / 2;
		int yOffset = y - modifier / 2 - 4;
		/*
		*if (isSwimming) {
		*	int waterColor = 0;
		*	yOffset += 4;
		*	if (tickCount % 60 < 15) {
		*		waterColor = Colors.get(-1, -1, 225, -1);
		*	} else if (15 <= tickCount % 60 && tickCount % 60 < 30) {
		*		yOffset -= 1;
		*		waterColor = Colors.get(-1, 225, 115, -1);
		*	} else if (30 <= tickCount % 60 && tickCount % 60 < 45) {
		*		waterColor = Colors.get(-1, 115, -1, 225);
		*	} else {
		*		yOffset -= 1;
		*		waterColor = Colors.get(-1, 225, 115, -1);
		*	}
		*	screen.render(xOffset, yOffset + 3, 0 + 27 * 32, waterColor, 0x00,
		*			1);
		*	screen.render(xOffset + 8, yOffset + 3, 0 + 27 * 32, waterColor,
		*			0x01, 1);
		*}
		*/
		screen.render(xOffset + modifier - (modifier * flipTop), yOffset, xTile	+ 1 + yTile * 32, color, flipTop, scale);
		screen.render(xOffset + (modifier * flipTop), yOffset, (xTile) + yTile * 32, color, flipTop, scale);

		if (!isSwimming) {
			screen.render(xOffset + modifier - (modifier * flipBottom), yOffset
					+ modifier, xTile + 1 + (yTile + 1) * 32, color,
					flipBottom, scale);
			screen.render(xOffset + (modifier * flipBottom),
					yOffset + modifier, (xTile) + (yTile + 1) * 32, color,
					flipBottom, scale);
		}
		
		//Heart rendering
		if(health > 0){
			if ((double) (maxHealth / health) >= 1 && (double) (maxHealth / health) < 2) {
			screen.render(xOffset + 4, yOffset + 17, 31 + 29 * 32,
					Colors.get(-1, 300, 500, 555), 0x00, scale);
			} else if ((double) (maxHealth / health) >= 2 && (double) (maxHealth / health) < 3) {
			screen.render(xOffset + 4, yOffset + 17, 31 + 29 * 32,
					Colors.get(-1, 200, 400, 444), 0x00, scale);
			} else if ((double) (maxHealth / health) >= 3 && (double) (maxHealth / health) < 4) {
			screen.render(xOffset + 4, yOffset + 17, 31 + 29 * 32,
					Colors.get(-1, 100, 300, 333), 0x00, scale);
			} else {
			screen.render(xOffset + 4, yOffset + 17, 31 + 29 * 32,
					Colors.get(-1, 000, 200, 222), 0x00, scale);
			}
		}
		if (username != null) {
			String usernamehp = username + " - " +  health;
			Font.render(usernamehp, screen, xOffset - (usernamehp.length() - 1) / 2
					* 8, yOffset - 10, Colors.get(-1, -1, -1, 555), 1);
		}
		if(input.mouse.rightIsHeld()&&(timeNow - lastSwing >= 500)&&!heldSwing&&!input.mouse.isHeld()){
			lastSwing = timeNow;
			heldSwing = true;
			hasSwung = true;
		}else if (!input.mouse.isHeld()){
			heldSwing = false;
		}
		//System.out.println(hasSwung);
		if(hasSwung){
			timeAftBegSwing = System.currentTimeMillis();
			if(shouldIterate){
				begSwing = System.currentTimeMillis();
				shouldIterate = false;
			}
			if(timeAftBegSwing - begSwing <= 200){
				xTile+=2;
			}else{
				hasSwung = false;
				shouldIterate = true;
			}
		}
		if(flipTop == 1){
			screen.render(xOffset + modifier - (modifier * flipBottom)-8, yOffset
					+ modifier, xTile + 3 + (yTile + 1) * 32, wandColor,
					flipBottom, scale);
			screen.render(xOffset + (modifier * flipBottom)-8,
					yOffset + modifier, (xTile + 2) + (yTile + 1) * 32, wandColor,
					flipBottom, scale);
			screen.render(xOffset + modifier - (modifier * flipTop)-8, yOffset, xTile	+ 3 + yTile * 32, wandColor, flipTop, scale);
			screen.render(xOffset + (modifier * flipTop)-8, yOffset, (xTile+2) + yTile * 32, wandColor, flipTop, scale);
		}else{
			screen.render(xOffset + modifier - (modifier * flipBottom)+8, yOffset
					+ modifier, xTile + 3 + (yTile + 1) * 32, wandColor,
					flipBottom, scale);
			screen.render(xOffset + (modifier * flipBottom)+8,
					yOffset + modifier, (xTile + 2) + (yTile + 1) * 32, wandColor,
					flipBottom, scale);
			screen.render(xOffset + modifier - (modifier * flipTop)+8, yOffset, xTile	+ 3 + yTile * 32, wandColor, flipTop, scale);
			screen.render(xOffset + (modifier * flipTop)+8, yOffset, (xTile+2) + yTile * 32, wandColor, flipTop, scale);
		}
		xTile -= 2;
	}
}
